﻿package model.drawPanel;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Observer;

import org.eclipse.swt.graphics.Image;

public class DrawNetModel implements IDrawNetModel {
	public static int drawServerUdpPort = 10810;//绘图信息专用端口号
	public static int drawClientUdpPort = 10820;
	public static int drawServerTcpPort = 10830;
	public static int drawClientTcpPort = 10840;
	public static int drawServerInitePort = 10850;
	public static int drawClientInitePort = 10860;
	public static String serverIP = "192.168.1.106";
	private IReceiver receiver = null;
	private DatagramSocket udp;
	private ObjectInputStream in;
	private ObjectOutputStream out;
	private Socket s ;
	private int roomID;
	private boolean status= true;
	private Thread udpReceiveThread;
	private Thread tcpReceiveThread;
	public DrawNetModel(int roomID){
			this.roomID = roomID;
			try {
				udp = new DatagramSocket();
			} catch (SocketException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			startUDPReceiveThread();
			startTCPReceiveThread();
	}
	private void startTCPReceiveThread(){
			 tcpReceiveThread = new Thread(new Runnable(){
				
				public void run(){
					try {
						s = new Socket(InetAddress.getByName(serverIP),drawServerTcpPort);
						out = new ObjectOutputStream(s.getOutputStream());
						in =new ObjectInputStream(s.getInputStream());
						out.writeObject(roomID);
					} catch (UnknownHostException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						e1.printStackTrace();
						return;
					}
					while(status){
						try {
							receiver.receive(in.readObject());
						} catch (ClassNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							return;
						}
					}
				}
			},"tcpRecvThread");
			tcpReceiveThread.start();
	}
	private void startUDPReceiveThread(){
			udpReceiveThread = new Thread(new Runnable(){
				@Override
				public void run() {
					try {
						udp = new DatagramSocket(drawClientUdpPort);
					} catch (SocketException e1) {
						e1.printStackTrace();
					}
						while(status){
							try {
								byte[] buf = new byte[8000];
								DatagramPacket packet  = new DatagramPacket(buf, buf.length);
								udp.receive(packet);
								receiver.receive((byte_to_object(buf)));
							} catch (IOException e) {
								return;
							}
						}
				}
			},"udpRecvThread");
			udpReceiveThread.start();
	}
	private byte[] object_to_byte (Object object){
			ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
			try {
				ObjectOutputStream buffers = new ObjectOutputStream(byteBuffer);
				buffers.writeObject(object);
				return byteBuffer.toByteArray();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		return null;
		
	}
	private Object byte_to_object (byte[] data){
		ByteArrayInputStream byteBuffer = new ByteArrayInputStream(data);
		try {
			ObjectInputStream buffers;
			buffers = new ObjectInputStream(byteBuffer);
			Object result = buffers.readObject();
			buffers.close();
			return result;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			
		}
		return null;
		
	}
	
	@Override
	public synchronized void sendOrder(Object object) {
		
	((Order)object).setRoomID(roomID);
	
	
	//图片tcp传输
		/*if(object.getClass().getName().equals("model.drawPanel.DrawOrder_ADD") &&
			((DrawOrder_ADD)object).getEntity().getClass().getName().equals("model.drawPanel.entitys.Picture")){
			try {
				out.writeObject(object);
				return;
		} catch (IOException e) {
				// TODO Auto-generated catch block
		e.printStackTrace();
			}
		}*/
		
	//*************************
	try {
		out.writeObject(object);
		return;
} catch (IOException e) {
		// TODO Auto-generated catch block
e.printStackTrace();                                                  
	}
	/*byte[]buf;
		buf = object_to_byte(object);
		if(buf.length > 1024){
			try {
				out.writeObject(object);
				System.out.println("picture is tansfer ok");
				return;
		} catch (IOException e) {
				// TODO Auto-generated catch block
		e.printStackTrace();
			}
		
		}
		DatagramPacket packet  = new DatagramPacket(buf, buf.length);
		try {
			packet.setAddress(InetAddress.getByName(serverIP));
			packet.setPort(drawServerUdpPort);
			udp.send(packet);
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
	}

	@Override
	public void addReceiver(IReceiver receiver) {
		this.receiver = receiver;
		
	}
	public void exite(){
			status=false;
			udp.close();
			try {
				s.close();
			} catch (IOException e) {
			}
	}

}